home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / metamail / contrib / mms / mmstransl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-27  |  5.0 KB  |  200 lines

  1. /*////////////////////////////////////////////////////////////////////////
  2. Copyright (c) 1992 Electrotechnical Laboratry (ETL)
  3.  
  4. Permission to use, copy, modify, and distribute this material 
  5. for any purpose and without fee is hereby granted, provided 
  6. that the above copyright notice and this permission notice 
  7. appear in all copies, and that the name of ETL not be 
  8. used in advertising or publicity pertaining to this 
  9. material without the specific, prior written permission 
  10. of an authorized representative of ETL.
  11. ETL MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY 
  12. OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS", 
  13. WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
  14. /////////////////////////////////////////////////////////////////////////
  15. Content-Type: program/C; charset=US-ASCII
  16. Program:      mmstransl.c (MIME message translator)
  17. Author:       Yutaka Sato <ysato@etl.go.jp>
  18. Description:
  19.  
  20. History:
  21.   v0.0  92.05.05     created a small prototype
  22. ///////////////////////////////////////////////////////////////////////*/
  23.  
  24. #include <stdio.h>
  25. typedef int MimePartP;
  26. typedef int (*IFUNCP)();
  27. #define STREQ(a,b)    (strcmp(a,b)==0)
  28.  
  29. /*/////////////////////////////////////////////////////////////////////*/
  30.  
  31. int MMS_translSh();
  32. int MMS_translDir();
  33. int MMS_translRTF();
  34. int MMS_translVin();
  35.  
  36. MMS_E_transl(ac,av,in,out)
  37.     char *av[];
  38. {    IFUNCP translator;
  39.  
  40.     translator = MMS_translSh; /* :-) */
  41.     if( 1 < ac ){
  42.         char *lang;
  43.  
  44.         lang = &av[1][1];
  45.         if(STREQ(lang,"sh" )) translator = MMS_translSh;  else
  46.         if(STREQ(lang,"dir")) translator = MMS_translDir; else
  47.         if(STREQ(lang,"rtf")) translator = MMS_translRTF; else
  48.         if(STREQ(lang,"vin")) translator = MMS_translVin;
  49.     }
  50.     MMS_transl(in,out,translator);
  51. }
  52. int (*MMS_E_TRANSL)() = MMS_E_transl;
  53.  
  54. /*/////////////////////////////////////////////////////////////////////*/
  55. #define MMS_T_START        0
  56. #define MMS_T_END        1
  57. #define MMS_T_PART        2
  58. #define MMS_T_MULTIPART_BEGIN    3
  59. #define MMS_T_MULTIPART_END    4
  60.  
  61. MMS_transl(in,out,translator)
  62.     FILE *in,*out;
  63.     IFUNCP translator;
  64. {    MimePartP root;
  65.  
  66.     if( !MMS_seekable(in) )
  67.         in = (FILE*)MMS_saveTempfile(in,0);
  68.     root = MMS_parser(in,out);
  69.     (*translator)(in,out,root,MMS_T_START);
  70.     MMS_translPart(in,out,root,translator);
  71.     (*translator)(in,out,root,MMS_T_END);
  72. }
  73.  
  74.  
  75. MMS_translPart(in,out,mp,translator)
  76.     FILE *in,*out;
  77.     MimePartP mp;
  78.     IFUNCP translator;
  79. {
  80.     if( MMS_partIsMultipart(mp) ){
  81.         (*translator)(in,out,mp,MMS_T_MULTIPART_BEGIN);
  82.         mp = MMS_translParts(in,out,mp,translator);
  83.         (*translator)(in,out,mp,MMS_T_MULTIPART_END);
  84.     }else{
  85.         (*translator)(in,out,mp,MMS_T_PART);
  86.     }
  87.     return mp;
  88. }
  89.  
  90. MMS_translParts(in,out,mp,translator)
  91.     FILE *in,*out;
  92.     IFUNCP translator;
  93. {
  94.     mp = MMS_part1stChild(mp);
  95.     while( mp ){
  96.         MMS_translPart(in,out,mp,translator);
  97.         mp = MMS_partNextPeer(mp);
  98.     }
  99.     return mp;
  100. }
  101.  
  102. /*/////////////////////////////////////////////////////////////////////
  103.  *    MIME to Sh (shell script)
  104.  */
  105. static char *MMS_T_END_Sh = "\
  106. #####################\n\
  107. PARTq(){ rm -rf $MMS_TMPFILES; exit; }\n\
  108. NEXTPART=1\n\
  109. echo -n \"[enter part number or RETURN($NEXTPART) or q]\"\n\
  110. while read COMMAND\n\
  111. do\n\
  112.     if [ \"$COMMAND\" = '' ]; then\n\
  113.         COMMAND=$NEXTPART\n\
  114.         echo \"($COMMAND)\"\n\
  115.     fi\n\
  116.     eval \"PART$COMMAND\"\n\
  117.     echo -n \"[enter part number or RETURN($NEXTPART) or q]\"\n\
  118. done\n\
  119. PARTq\n\
  120. ";
  121. MMS_translSh(in,out,mp,where)
  122.     FILE *in,*out;
  123.     MimePartP mp;
  124. {    char holophrast[128];
  125.     char *decoder;
  126.     char viewer[1024];
  127.     char tmpfile[1024];
  128.     static char tmpid;
  129.     int usetemp;
  130.     int serial;
  131.  
  132.     switch( where ){
  133.     case MMS_T_START: break;
  134.     case MMS_T_END: fprintf(out,"%s",MMS_T_END_Sh); break;
  135.  
  136.     case MMS_T_PART:
  137.     case MMS_T_MULTIPART_BEGIN:
  138.         serial = MMS_partSerialNO(mp);
  139.         MMS_holophrast(holophrast,mp);
  140.         fprintf(out,"########### (%d)  %s\n",serial,holophrast);
  141.         MIME_dumpValues(out,mp,"# ");
  142.         fprintf(out,"echo '(%d)  %s'\n",serial,holophrast);
  143.         fprintf(out,"PART%d(){\n",serial);
  144.  
  145.         if( where == MMS_T_PART ){
  146.             sprintf(tmpfile,"/tmp/mms.$$.%d",tmpid);
  147.             if( usetemp = MMS_BuildViewer(in,mp,viewer,tmpfile) ){
  148.                 fprintf(out,
  149.                     "MMS_TMPFILES=\"$MMS_TMPFILES %s\"\n",
  150.                     tmpfile);
  151.                 tmpid++;
  152.             }
  153.             decoder = (char*)MIME_Decoder(mp);
  154.             if( decoder )
  155.                 fprintf(out,"(%s << EOT\n",decoder);
  156.             else    fprintf(out,"(cat << EOT\n");
  157.  
  158.             MMS_partWriteBody(in,out,mp,0,0);
  159.  
  160.             if( usetemp )
  161.                 fprintf(out,"EOT\n)>%s\n%s\n",tmpfile,viewer);
  162.             else    fprintf(out,"EOT\n) | %s\n\n",viewer);
  163.         }else{
  164.             fprintf(out,"echo '%s'\n",holophrast);
  165.         }
  166.         if( MMS_partNext(mp) )
  167.             fprintf(out,"NEXTPART=%d\n",serial+1);
  168.         else    fprintf(out,"NEXTPART=q\n");
  169.         fprintf(out,"}\n");
  170.         break;
  171.     }
  172. }
  173.  
  174. /*/////////////////////////////////////////////////////////////////////
  175.  *    MIME to Directory Tree
  176.  */
  177. MMS_translDir(in,out,mp,where)
  178.     FILE *in,*out;
  179.     MimePartP mp;
  180. {
  181. }
  182.  
  183. /*/////////////////////////////////////////////////////////////////////
  184.  *    MIME to RTF (Rich Text Format)
  185.  */
  186. MMS_translRTF(in,out,mp,where)
  187.     FILE *in,*out;
  188.     MimePartP mp;
  189. {
  190. }
  191.  
  192. /*/////////////////////////////////////////////////////////////////////
  193.  *    MIME to Vin
  194.  */
  195. MMS_translVin(in,out,mp,where)
  196.     FILE *in,*out;
  197.     MimePartP mp;
  198. {
  199. }
  200.